package com.whlilong.jxc.view;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.ViewPart;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.input.SellBillEditorInput;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.util.ViewerChange;

/**
 * 商品管理视图
 * 
 * @author sjy
 * 
 */
public class ProductManagerView extends ViewPart implements ISelectionListener {

	private CLabel spdj; // 商品定价
	private CLabel arrow;
	private CLabel xsdj; // 销售单据
	private CLabel arrow_v;
	private CLabel spxs; // 商品销售
	private CLabel spth; // 商品退货
	private CLabel spdetail; // 商品销售明细
	private CLabel xsthdetail; // 销售退货明细

	private final Color blue = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLUE);
	private final Color black = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLACK);
	private final IWorkbenchWindow window = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow();

	public ProductManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		createContent(parent);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.addSelectionListener(this);
	}

	private void createContent(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 5;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		layout.marginRight = 10;
		layout.marginLeft = 10;
		layout.makeColumnsEqualWidth = false;

		composite.setLayout(layout);
		composite.setBackgroundImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.FUNCTIONS_BG));
		createFlowchart(composite);
	}

	private void createFlowchart(Composite parent) {
		// TODO Auto-generated method stub
		spdj = new CLabel(parent, SWT.NONE);
		GridData gd = new GridData(GridData.CENTER);
		gd.verticalSpan = 5;
		gd.widthHint = 100;
		gd.heightHint = 300;
		spdj.setText("商品定价");
		spdj.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		spdj
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		spdj.setLayoutData(gd);

		arrow = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalSpan = 5;
		gd.widthHint = 90;
		gd.heightHint = 300;
		arrow.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.arow));
		arrow.setBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		arrow.setLayoutData(gd);

		xsdj = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.horizontalSpan = 3;
		gd.widthHint = 120;
		gd.heightHint = 70;
		xsdj.setText("销售单据查询");
		xsdj.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.sellquery));
		xsdj
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		xsdj.setLayoutData(gd);

		arrow_v = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.horizontalSpan = 3;
		gd.widthHint = 120;
		gd.heightHint = 40;
		arrow_v.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.arow_v));
		arrow_v.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		arrow_v.setLayoutData(gd);

		spxs = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.horizontalIndent = 0;
		gd.horizontalIndent = 10;
		gd.widthHint = 100;
		gd.heightHint = 50;
		spxs.setText("商品销售");
		spxs.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.PIFAXIAOSHOU_ICON));
		spxs
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		spxs.setLayoutData(gd);

		arrow = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalSpan = 5;
		gd.widthHint = 90;
		arrow.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.arow));
		arrow.setBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		arrow.setLayoutData(gd);

		spth = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.horizontalIndent = 0;
		gd.horizontalIndent = 10;
		gd.widthHint = 120;
		gd.heightHint = 50;
		spth.setText("商品退货");
		spth.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		spth
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		spth.setLayoutData(gd);

		arrow_v = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.widthHint = 120;
		gd.heightHint = 40;
		arrow_v.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.arow_v1));
		arrow_v.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		arrow_v.setLayoutData(gd);

		arrow_v = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.widthHint = 120;
		gd.heightHint = 40;
		arrow_v.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.arow_v1));
		arrow_v.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		arrow_v.setLayoutData(gd);

		spdetail = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 0;
		gd.horizontalIndent = 10;
		gd.widthHint = 150;
		gd.heightHint = 50;
		spdetail.setText("商品销售明细");
		spdetail.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		spdetail.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		spdetail.setLayoutData(gd);

		xsthdetail = new CLabel(parent, SWT.NONE);
		gd = new GridData(GridData.CENTER);
		gd.horizontalIndent = 0;
		gd.verticalIndent = 0;
		gd.horizontalIndent = 10;
		gd.widthHint = 150;
		gd.heightHint = 50;
		xsthdetail.setText("销售退货明细");
		xsthdetail.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		xsthdetail.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		xsthdetail.setLayoutData(gd);

		addListener();

	}

	private void addListener() {
		// TODO Auto-generated method stub
		spdj.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				spdj.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				spdj.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				spdj.setToolTipText(spdj.getText());
			}

		});
		xsdj.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				xsdj.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				xsdj.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				xsdj.setToolTipText(xsdj.getText());
			}

		});
		xsdj.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				SellBillEditorInput input = new SellBillEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.sellBillEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});
		spxs.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				spxs.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				spxs.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				spxs.setToolTipText(spxs.getText());
			}

		});
		spth.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				spth.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				spth.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				spth.setToolTipText(spth.getText());
			}

		});
		spdetail.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				spdetail.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				spdetail.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				spdetail.setToolTipText(spdetail.getText());
			}

		});
		xsthdetail.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				xsthdetail.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				xsthdetail.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				xsthdetail.setToolTipText(xsthdetail.getText());
			}

		});

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (!selection.isEmpty()) {
			IViewPart vp = getViewSite().getPage().findView(
					IAppConstants.systemManagerview_id);

			Table table = ((SystemManagerView) vp).getTableViewer().getTable();
			ViewerChange.getInstance().refreshView(table);
		}
	}

}
